package com.project.lk.nurseapp.utils;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;

import com.project.lk.nurseapp.base.BaseApp;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;

import static com.project.lk.nurseapp.common.view.chat.voice.StorageUtil.isStorageAvailable;


/**
 * 功能介绍：
 * 项目名：
 * 模块名称：
 * 作者：LK
 * 日期：2018/1/9
 * 修改日期：
 */

public class MediaUtils {
    public static String getRingDuring(String mUri) {
        String duration = null;
        android.media.MediaMetadataRetriever mmr = new android.media.MediaMetadataRetriever();

        try {
            if (mUri != null) {
                HashMap<String, String> headers = null;
                if (headers == null) {
                    headers = new HashMap<String, String>();
                    headers.put("User-Agent", "Mozilla/5.0 (Linux; U; Android 4.4.2; zh-CN; MW-KW-001 Build/JRO03C) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 UCBrowser/1.0.0.001 U4/0.8.0 Mobile Safari/533.1");
                }
                mmr.setDataSource(mUri, headers);
            }

            duration = mmr.extractMetadata(android.media.MediaMetadataRetriever.METADATA_KEY_DURATION);
        } catch (Exception ex) {

        } finally {
            mmr.release();
        }
        Log.e("MLOG", "duration " + duration);
        return duration;
    }

    public static String getAudioTime(String mUri) {
        MediaPlayer mp = MediaPlayer.create(BaseApp.getContext(), Uri.parse(mUri));
        if (mp.getDuration()<1000) {
            return String.valueOf(1000);
        }else {
            return String.valueOf(mp.getDuration());
        }
    }
    public static void playMp3(byte[] mp3SoundByteArray) {
        try {
            // create temp file that will hold byte array
//            File tempMp3 = File.createTempFile("voice", "mp3",  new File(getSDFilePath()));
//            if (tempMp3!=null) {
//                tempMp3.deleteOnExit();
//            }
            File file = new File(getSDFilePath());
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(mp3SoundByteArray);
            fos.close();

            // Tried reusing instance of media player
            // but that resulted in system crashes...
            MediaPlayer mediaPlayer = new MediaPlayer();

            // Tried passing path directly, but kept getting
            // "Prepare failed.: status=0x1"
            // so using file descriptor instead
            FileInputStream fis = new FileInputStream(file);
            mediaPlayer.setDataSource(fis.getFD());

            mediaPlayer.prepareAsync();
            mediaPlayer.start();
        } catch (IOException ex) {
           Log.e("MLOG",ex.getMessage());
        }
    }
    public static String getSDFilePath(){
        if(isStorageAvailable()){
            return Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+"voice.mp3";
        }else{
            return null;
        }
    }
}
